<?php 
	session_start();
	error_reporting(0);
	require 'db.php';
	require 'Page.class.php';



/****************分数排行榜*************************/
	//专业组
	$sql ="SELECT * FROM oc_sport_academy WHERE status='1' AND is_professional='0'";
	$query=mysql_query($sql);
	if($query&&mysql_num_rows($query)){
			while($row=mysql_fetch_assoc($query)){
				$data[]=$row;
		}
	}
	foreach($data as $val){
		$academy=$val['academy'];
		$sql="SELECT score FROM oc_sport_score WHERE academy='$academy' AND status='1'";
		$score[$academy]='';
		$query=mysql_query($sql);
		if($query&&mysql_num_rows($query)){
			while($row=mysql_fetch_assoc($query)){
				if($row['score']){
					$score[$academy]+=$row['score'];
				}
			}
		}else{
			$score[$academy]='0';
		}
	}
	arsort($score);  //数据排序


	//非专业组
	$sql_p ="SELECT * FROM oc_sport_academy WHERE status='1' AND is_professional='1'";
	$query_p=mysql_query($sql_p);
	if($query_p&&mysql_num_rows($query_p)){
			while($row_p=mysql_fetch_assoc($query_p)){
				$data_p[]=$row_p;
		}
	}
	foreach($data_p as $val){
		$academy=$val['academy'];
		$sql="SELECT score FROM oc_sport_score WHERE academy='$academy' AND status='1'";
		$score1[$academy]='';
		$query=mysql_query($sql);
		if($query&&mysql_num_rows($query)){
			while($row=mysql_fetch_assoc($query)){
				if($row['score']){
					$score1[$academy]+=$row['score'];
				}
			}
		}else{
			$score1[$academy]='0';
		}
	}
	arsort($score1);  //数据排序

/****************END*************************/



/****************公告详情*************************/
	if(@$_GET['s']=='a'&&!empty($_GET['p'])){
		$countsql="SELECT count(*) as count FROM oc_sport_score WHERE status='1'";
		$query=mysql_query($countsql);
		$count=mysql_num_rows($query);
		if($rs=mysql_fetch_array($query)){
			$total=$rs[0];
		}else{
			$total=0;
		}
		$countN=ceil($total/8);

		$p = !empty($_GET['p']) ? $_GET['p'] : '1';
		$first = ($p-1)*8; 
		$sql ="SELECT * FROM oc_sport_score WHERE status='1' ORDER BY id desc LIMIT $first,8";
		$query=mysql_query($sql);
		if($query&&mysql_num_rows($query)){
				while($row=mysql_fetch_assoc($query)){
					$notice[]=$row;
			}
		}
	}else{
		$countsql="SELECT count(*) FROM oc_sport_score WHERE status='1'";
		$query=mysql_query($countsql);
		$count=mysql_num_rows($query);
		if($rs=mysql_fetch_array($query)){
			$total=$rs[0];
		}else{
			$total=0;
		}
		$countN=ceil($total/8);

		$sql ="SELECT * FROM oc_sport_score WHERE status='1' ORDER BY id desc LIMIT 8";
		$query=mysql_query($sql);
		if($query&&mysql_num_rows($query)){
				while($row=mysql_fetch_assoc($query)){
					$notice[]=$row;
			}
		}
	}


		
/****************END*************************/


/***************留言详情************************/	
	if(@$_GET['s']=='m'&&!empty($_GET['p'])){
		$countsql="SELECT count(*) FROM oc_sport_message WHERE status='1'";
		$query=mysql_query($countsql);
		$count=mysql_num_rows($query);
		if($rs=mysql_fetch_array($query)){
			$total=$rs[0];
		}else{
			$total=0;
		}
		$countM=ceil($total/9);

		$p = !empty($_GET['p']) ? $_GET['p'] : '1';
		$first = ($p-1)*9; 
		$sql ="SELECT * FROM oc_sport_message WHERE status='1' ORDER BY id desc LIMIT $first,9";
		$query=mysql_query($sql);
		if($query&&mysql_num_rows($query)){
				while($row=mysql_fetch_assoc($query)){
					$message[]=$row;
			}
		}
	}else{
		$countsql="SELECT count(*) FROM oc_sport_message WHERE status='1'";
		$query=mysql_query($countsql);
		$count=mysql_num_rows($query);
		if($rs=mysql_fetch_array($query)){
			$total=$rs[0];
		}else{
			$total=0;
		}
		$countM=ceil($total/9);

		$sql ="SELECT * FROM oc_sport_message WHERE status='1' ORDER BY id desc LIMIT 9";
		$query=mysql_query($sql);
		if($query&&mysql_num_rows($query)){
				while($row=mysql_fetch_assoc($query)){
					$message[]=$row;
			}
		}
	}
		
/***************END************************/

/****************新闻详情*************************/
	if(@$_GET['s']=='n'&&!empty($_GET['p'])){
		$countsql="SELECT count(*) FROM oc_sport_news WHERE status='1'";
		$query=mysql_query($countsql);
		$count=mysql_num_rows($query);
		if($rs=mysql_fetch_array($query)){
			$total=$rs[0];
		}else{
			$total=0;
		}
		$countW=ceil($total/9);

		$p = !empty($_GET['p']) ? $_GET['p'] : '1';
		$first = ($p-1)*9; 
		$sql ="SELECT * FROM oc_sport_news WHERE status='1' ORDER BY id desc LIMIT $first,9";
		$query=mysql_query($sql);
		if($query&&mysql_num_rows($query)){
				while($row=mysql_fetch_assoc($query)){
					$news[]=$row;
			}
		}
	}else{
		$countsql="SELECT count(*) FROM oc_sport_news WHERE status='1'";
		$query=mysql_query($countsql);
		$count=mysql_num_rows($query);
		if($rs=mysql_fetch_array($query)){
			$total=$rs[0];
		}else{
			$total=0;
		}
		$countW=ceil($total/9);

		$sql ="SELECT * FROM oc_sport_news WHERE status='1' ORDER BY id desc LIMIT 9";
		$query=mysql_query($sql);
		if($query&&mysql_num_rows($query)){
				while($row=mysql_fetch_assoc($query)){
					$news[]=$row;
			}
		}
	}
		
/****************END*************************/



/****************点赞详情*************************/
	$sql ="SELECT * FROM oc_sport_academy WHERE status='1' ORDER BY cheer desc";
	$query=mysql_query($sql);
	if($query&&mysql_num_rows($query)){
			while($row=mysql_fetch_assoc($query)){
				$cheer[]=$row;
		}
	}
		
/****************END*************************/


/****************破纪录详情*************************/
	$sql ="SELECT * FROM oc_sport_score WHERE status='1' AND is_record='1' ORDER BY id desc";
	$query=mysql_query($sql);
	if($query&&mysql_num_rows($query)){
			while($row=mysql_fetch_assoc($query)){
				$record[]=$row;
		}
	}
		
/****************END*************************/

/****************访问量*************************/
	$totalsql="UPDATE oc_sport_ip SET total=total+1 WHERE id='1'";
	$query=mysql_query($totalsql);
	$ip=$_SERVER["REMOTE_ADDR"];
	$ipsql="SELECT count(*) as count FROM oc_sport_ip WHERE ip='$ip'";
	$query=mysql_query($ipsql);
	$count=mysql_num_rows($query);
	if($rs=mysql_fetch_array($query)){
		$totalip=$rs[0];
	}else{
		$totalip=0;
	}
	if(!$totalip){
		$sqlip="INSERT INTO oc_sport_ip (ip) VALUES ('$ip')";
		$query=mysql_query($sqlip);
	}
	$ipsql1="SELECT count(*) as count FROM oc_sport_ip";
	$query=mysql_query($ipsql1);
	$count=mysql_num_rows($query);
	if($rs=mysql_fetch_array($query)){
		$totalall=$rs[0];
	}else{
		$totalall=0;
	}
	$countsql="SELECT total FROM oc_sport_ip WHERE id='1'";
	$webcount=mysql_query($countsql);
	if($webcount&&mysql_num_rows($webcount)){
			while($row=mysql_fetch_assoc($webcount)){
				$web[]=$row;
		}
	}
	$countsqlp="SELECT total FROM oc_sport_ip WHERE ip='$ip'";
	$webp=mysql_query($countsqlp);
	if($webp&&mysql_num_rows($webp)){
			while($row=mysql_fetch_assoc($webp)){
				$totalp[]=$row;
		}
	}
	
	mysql_close($link);
/**	if(@$_GET['s']=='a'&&!empty($_GET['p'])){
		$countsql="SELECT count(*) as count FROM oc_sport_score WHERE status='1'";
		$query=mysql_query($countsql);
		$count=mysql_num_rows($query);
		if($rs=mysql_fetch_array($query)){
			$total=$rs[0];
		}else{
			$total=0;
		}
		$countN=ceil($total/8);

		$p = !empty($_GET['p']) ? $_GET['p'] : '1';
		$first = ($p-1)*8; 
		$sql ="SELECT * FROM oc_sport_score WHERE status='1' ORDER BY id desc LIMIT $first,8";
		$query=mysql_query($sql);
		if($query&&mysql_num_rows($query)){
				while($row=mysql_fetch_assoc($query)){
					$notice[]=$row;
			}
		}
	}else{
		$countsql="SELECT count(*) FROM oc_sport_score WHERE status='1'";
		$query=mysql_query($countsql);
		$count=mysql_num_rows($query);
		if($rs=mysql_fetch_array($query)){
			$total=$rs[0];
		}else{
			$total=0;
		}
		$countN=ceil($total/8);

		$sql ="SELECT * FROM oc_sport_score WHERE status='1' ORDER BY id desc LIMIT 8";
		$query=mysql_query($sql);
		if($query&&mysql_num_rows($query)){
				while($row=mysql_fetch_assoc($query)){
					$notice[]=$row;
			}
		}
	}

**/
		
/****************END*************************/

    /**
     * 友好的时间显示
     *
     * @param int    $sTime 待显示的时间
     * @param string $type  类型. normal | mohu | full | ymd | other
     * @param string $alt   已失效
     * @return string
     */
    function friendlyDate($sTime,$type = 'normal',$alt = 'false') {
        //sTime=源时间，cTime=当前时间，dTime=时间差
        $cTime        =    time();
        $dTime        =    $cTime - $sTime;
        $dDay        =    intval(date("z",$cTime)) - intval(date("z",$sTime));
        //$dDay        =    intval($dTime/3600/24);
        $dYear        =    intval(date("Y",$cTime)) - intval(date("Y",$sTime));
        //normal：n秒前，n分钟前，n小时前，日期
        if($type=='normal'){
            if( $dTime < 60 ){
                return $dTime."秒前";
            }elseif( $dTime < 3600 ){
                return intval($dTime/60)."分钟前";
            //今天的数据.年份相同.日期相同.
            }elseif( $dYear==0 && $dDay == 0  ){
                //return intval($dTime/3600)."小时前";
                return '今天'.date('H:i',$sTime);
            }elseif($dYear==0){
                return date("m月d日 H:i",$sTime);
            }else{
                return date("Y-m-d H:i",$sTime);
            }
        }elseif($type=='mohu'){
            if( $dTime < 60 ){
                return $dTime."秒前";
            }elseif( $dTime < 3600 ){
                return intval($dTime/60)."分钟前";
            }elseif( $dTime >= 3600 && $dDay == 0  ){
                return intval($dTime/3600)."小时前";
            }elseif( $dDay > 0 && $dDay<=7 ){
                return intval($dDay)."天前";
            }elseif( $dDay > 7 &&  $dDay <= 30 ){
                return intval($dDay/7) . '周前';
            }elseif( $dDay > 30 ){
                return intval($dDay/30) . '个月前';
            }
        //full: Y-m-d , H:i:s
        }elseif($type=='full'){
            return date("Y-m-d , H:i:s",$sTime);
        }elseif($type=='ymd'){
            return date("Y-m-d",$sTime);
        }else{
            if( $dTime < 60 ){
                return $dTime."秒前";
            }elseif( $dTime < 3600 ){
                return intval($dTime/60)."分钟前";
            }elseif( $dTime >= 3600 && $dDay == 0  ){
                return intval($dTime/3600)."小时前";
            }elseif($dYear==0){
                return date("Y-m-d H:i:s",$sTime);
            }else{
                return date("Y-m-d H:i:s",$sTime);
            }
        }
    }


    function ubbReplace($str) {
	    $str = str_replace ( ">", '<；', $str ); 
	    $str = str_replace ( ">", '>；', $str );
	    $str = str_replace ( "\n", '>；br/>；', $str );   
	    $str = preg_replace ( "[\[em_(.*?)\]]", "<img src=\"/bmxt/arclist/$1.gif\" />", $str );
	    return $str;
	}
?>